import copy
import time


class HTimer():
    def __init__(self):
        self._times_dict = {}

        self._current_qs = -1

        self._start_time = time.time()
        self._end_time = time.time()

    def set_qs_type(self, qs):
        self._current_qs = qs

    def start_time(self):
        self._start_time = time.time()

    def end_time(self):
        if self._current_qs == -1:
            return
        self._end_time = time.time()
        dat = float(self._end_time - self._start_time) * 1000.0

        if dat < 0:
            return

        if self._current_qs not in self._times_dict:
            self._times_dict[self._current_qs] = 0.0
        self._times_dict[self._current_qs] += dat

    def reset(self):
        self._times_dict = {}

    def gettime(self):
        vs = self._times_dict.values()
        t = 0
        for i in vs:
            t += i

        res = copy.copy(self._times_dict)
        res["total"] = t
        return res


htimer = HTimer()
